<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>珠峰架构师成长计划</title>
    <link rel="stylesheet" type="text/css" href="../static/css/main.css">
</head>
<body>
<div class="nav">
    <div class="logo">
        
            珠峰架构师成长计划
        
    </div>
<ul><li><a href="../index.html">0.Async</a></li><li><a href="../html/0.editor.html">0.editor</a></li><li><a href="../html/0.module.html">0.module</a></li><li><a href="../html/1.ES2015.html">1.ES2015</a></li><li><a href="../html/2.Promise.html">2.Promise</a></li><li><a href="../html/3.Node.html">3.Node</a></li><li><a href="../html/4.NodeInstall.html">4.NodeInstall</a></li><li><a href="../html/5.REPL.html">5.REPL</a></li><li><a href="../html/6.NodeCore.html">6.NodeCore</a></li><li><a href="../html/7.module&NPM.html">7.module&NPM</a></li><li><a href="../html/8.Encoding.html">8.Encoding</a></li><li><a href="../html/9.Buffer.html">9.Buffer</a></li><li><a href="../html/10.fs.html">10.fs</a></li><li><a href="../html/11.Stream-1.html">11.Stream-1</a></li><li><a href="../html/11.Stream-2.html">11.Stream-2</a></li><li><a href="../html/11.Stream-3.html">11.Stream-3</a></li><li><a href="../html/11.Stream-4.html">11.Stream-4</a></li><li><a href="../html/12-Network-2.html">12-Network-2</a></li><li><a href="../html/12.NetWork-3.html">12.NetWork-3</a></li><li><a href="../html/12.Network-1.html">12.Network-1</a></li><li><a href="../html/13.tcp.html">13.tcp</a></li><li><a href="../html/14.http-1.html">14.http-1</a></li><li><a href="../html/14.http-2.html">14.http-2</a></li><li><a href="../html/15.compress.html">15.compress</a></li><li><a href="../html/16.crypto.html">16.crypto</a></li><li><a href="../html/17.process.html">17.process</a></li><li><a href="../html/18.yargs.html">18.yargs</a></li><li><a href="../html/19.cache.html">19.cache</a></li><li><a href="../html/20.action.html">20.action</a></li><li><a href="../html/21.https.html">21.https</a></li><li><a href="../html/22.cookie.html">22.cookie</a></li><li><a href="../html/23.session.html">23.session</a></li><li><a href="../html/24.express-1.html">24.express-1</a></li><li><a href="../html/24.express-2.html">24.express-2</a></li><li><a href="../html/24.express-3.html">24.express-3</a></li><li><a href="../html/24.express-4.html">24.express-4</a></li><li><a href="../html/25.koa-1.html">25.koa-1</a></li><li><a href="../html/26.webpack-1-basic.html">26.webpack-1-basic</a></li><li><a href="../html/26.webpack-2-optimize.html">26.webpack-2-optimize</a></li><li><a href="../html/26.webpack-3.tapable.html">26.webpack-3.tapable</a></li><li><a href="../html/26.webpack-4-AST.html">26.webpack-4-AST</a></li><li><a href="../html/26.webpack-5-source.html">26.webpack-5-source</a></li><li><a href="../html/26.webpack-6-loader.html">26.webpack-6-loader</a></li><li><a href="../html/26.webpack-7-plugin.html">26.webpack-7-plugin</a></li><li><a href="../html/26.webpack-8-hand.html">26.webpack-8-hand</a></li><li><a href="../html/27.react-1.html">27.react-1</a></li><li><a href="../html/27.react-2.html">27.react-2</a></li><li class="active"><a href="../html/27.react-3.html">27.react-3</a></li><li><a href="../html/27.react-4-immutable.html">27.react-4-immutable</a></li><li><a href="../html/27.react-5-react-dom-diff.html">27.react-5-react-dom-diff</a></li><li><a href="../html/27.react-6.html">27.react-6</a></li><li><a href="../html/28.react-mobx.html">28.react-mobx</a></li><li><a href="../html/28.redux-0.html">28.redux-0</a></li><li><a href="../html/28.redux-1.html">28.redux-1</a></li><li><a href="../html/28.redux-2-中间件.html">28.redux-2-中间件</a></li><li><a href="../html/28.redux-3-saga.html">28.redux-3-saga</a></li><li><a href="../html/28.redux-jwt-back.html">28.redux-jwt-back</a></li><li><a href="../html/28.redux-jwt-front.html">28.redux-jwt-front</a></li><li><a href="../html/29.mongodb-1.html">29.mongodb-1</a></li><li><a href="../html/29.mongodb-2.html">29.mongodb-2</a></li><li><a href="../html/29.mongodb-3.html">29.mongodb-3</a></li><li><a href="../html/29.mongodb-4.html">29.mongodb-4</a></li><li><a href="../html/29.mongodb-5.html">29.mongodb-5</a></li><li><a href="../html/29.mongodb-6.html">29.mongodb-6</a></li><li><a href="../html/30.cms-1-mysql.html">30.cms-1-mysql</a></li><li><a href="../html/30.cms-2-mysql.html">30.cms-2-mysql</a></li><li><a href="../html/30.cms-3-mysql.html">30.cms-3-mysql</a></li><li><a href="../html/30.cms-4-egg.html">30.cms-4-egg</a></li><li><a href="../html/30.cms-5-api.html">30.cms-5-api</a></li><li><a href="../html/30.cms-6-roadhog.html">30.cms-6-roadhog</a></li><li><a href="../html/30.cms-7-umi.html">30.cms-7-umi</a></li><li><a href="../html/30.cms-8-dva.html">30.cms-8-dva</a></li><li><a href="../html/30.cms-9-dva.html">30.cms-9-dva</a></li><li><a href="../html/30.cms-10-front.html">30.cms-10-front</a></li><li><a href="../html/30.cms-11-deploy.html">30.cms-11-deploy</a></li><li><a href="../html/33.redis.html">33.redis</a></li><li><a href="../html/34.unittest.html">34.unittest</a></li><li><a href="../html/35.jwt.html">35.jwt</a></li><li><a href="../html/36.websocket-1.html">36.websocket-1</a></li><li><a href="../html/36.websocket-2.html">36.websocket-2</a></li><li><a href="../html/38.chat-api-1.html">38.chat-api-1</a></li><li><a href="../html/38.chat-api-2.html">38.chat-api-2</a></li><li><a href="../html/38.chat-3.html">38.chat-3</a></li><li><a href="../html/38.chat-api-3.html">38.chat-api-3</a></li><li><a href="../html/38.chat.html">38.chat</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/39.crawl-0.html">39.crawl-0</a></li><li><a href="../html/39.crawl-1.html">39.crawl-1</a></li><li><a href="../html/39.crawl-2.html">39.crawl-2</a></li><li><a href="../html/40.deploy.html">40.deploy</a></li><li><a href="../html/41.safe.html">41.safe</a></li><li><a href="../html/42.test.html">42.test</a></li><li><a href="../html/43.nginx.html">43.nginx</a></li><li><a href="../html/44.enzyme.html">44.enzyme</a></li><li><a href="../html/45.docker.html">45.docker</a></li><li><a href="../html/46.elastic.html">46.elastic</a></li><li><a href="../html/47.oauth.html">47.oauth</a></li><li><a href="../html/48.wxpay.html">48.wxpay</a></li><li><a href="../html/49.nunjucks.html">49.nunjucks</a></li><li><a href="../html/50.ketang.html">50.ketang</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/51.typescript.html">51.typescript</a></li><li><a href="../html/52.UML.html">52.UML</a></li><li><a href="../html/53.design.html">53.design</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/54.linux.html">54.linux</a></li><li><a href="../html/55.yaml.html">55.yaml</a></li><li><a href="../html/50.ketang2.html">50.ketang2</a></li><li><a href="../html/56.ts.html">56.ts</a></li><li><a href="../html/57.ts_react.html">57.ts_react</a></li><li><a href="../html/58.react-ssr.html">58.react-ssr</a></li><li><a href="../html/59.react-ssr.html">59.react-ssr</a></li></ul></div>


<div class="warpper">

    <div class="page-toc">
        <ul><li><a href="#t01. React路由">1. React路由</a><ul><li><a href="#t11.1 hash">1.1 hash</a></li><li><a href="#t21.2 history">1.2 history</a></li></ul></li><li><a href="#t32.跑通路由">2.跑通路由</a><ul><li><a href="#t42.1 index.js">2.1 index.js</a></li><li><a href="#t52.2 Home.js">2.2 Home.js</a></li><li><a href="#t62.3 Profile.js">2.3 Profile.js</a></li><li><a href="#t72.4 User.js">2.4 User.js</a></li><li><a href="#t82.5 this.props">2.5 this.props</a></li><li><a href="#t92.6  createHref">2.6  createHref</a></li></ul></li><li><a href="#t103.实现基本路由">3.实现基本路由</a><ul><li><a href="#t113.1 index.js">3.1 index.js</a></li><li><a href="#t123.2 index.js">3.2 index.js</a></li><li><a href="#t133.3 context.js">3.3 context.js</a></li><li><a href="#t143.4 HashRouter.js">3.4 HashRouter.js</a></li><li><a href="#t153.5 Route.js">3.5 Route.js</a></li></ul></li><li><a href="#t164. path-to-regexp">4. path-to-regexp</a></li><li><a href="#t175. 正则匹配路径">5. 正则匹配路径</a><ul><li><a href="#t185.1 Route.js">5.1 Route.js</a></li></ul></li><li><a href="#t196.exact 精确匹配">6.exact 精确匹配</a><ul><li><a href="#t206.1 index.js">6.1 index.js</a></li><li><a href="#t216.2  Route.js">6.2  Route.js</a></li></ul></li><li><a href="#t227. Link">7. Link</a><ul><li><a href="#t237.1 Link.js">7.1 Link.js</a></li><li><a href="#t247.2 index.js">7.2 index.js</a></li><li><a href="#t257.3 HashRouter.js">7.3 HashRouter.js</a></li></ul></li><li><a href="#t268.Redirect&Switch">8.Redirect&Switch</a><ul><li><a href="#t278.1 index.js">8.1 index.js</a></li><li><a href="#t288.2 Redirect.js">8.2 Redirect.js</a></li><li><a href="#t298.3  Switch.js">8.3  Switch.js</a></li></ul></li><li><a href="#t309.页面跳转">9.页面跳转</a><ul><li><a href="#t319.1  User.js">9.1  User.js</a></li><li><a href="#t329.2 HashRouter.js">9.2 HashRouter.js</a></li><li><a href="#t339.3  Route.js">9.3  Route.js</a></li><li><a href="#t349.4  Switch.js">9.4  Switch.js</a></li><li><a href="#t359.5  UserAdd.js">9.5  UserAdd.js</a></li><li><a href="#t369.6 UserDetail.js">9.6 UserDetail.js</a></li><li><a href="#t379.7 UserList.js">9.7 UserList.js</a></li></ul></li><li><a href="#t3810. 受保护的路由">10. 受保护的路由</a><ul><li><a href="#t3910.1 src/index.js">10.1 src/index.js</a></li><li><a href="#t4010.2 HashRouter.js">10.2 HashRouter.js</a></li><li><a href="#t4110.3 Route.js">10.3 Route.js</a></li><li><a href="#t4210.4 Login.js">10.4 Login.js</a></li><li><a href="#t4310.5 Protected.js">10.5 Protected.js</a></li></ul></li><li><a href="#t4411. 自定义导航">11. 自定义导航</a><ul><li><a href="#t4511.1 index.js">11.1 index.js</a></li><li><a href="#t4611.2  Route.js">11.2  Route.js</a></li><li><a href="#t4711.3 MenuLink.js">11.3 MenuLink.js</a></li><li><a href="#t4811.4  MenuLink.css">11.4  MenuLink.css</a></li></ul></li><li><a href="#t4912. 防止跳转">12. 防止跳转</a><ul><li><a href="#t5012.1 UserAdd.js">12.1 UserAdd.js</a></li><li><a href="#t5112.2  HashRouter.js">12.2  HashRouter.js</a></li><li><a href="#t5212.3 Prompt.js">12.3 Prompt.js</a></li></ul></li><li><a href="#t5313. withRouter">13. withRouter</a><ul><li><a href="#t5413.1 index.js">13.1 index.js</a></li><li><a href="#t5513.2 Route.js">13.2 Route.js</a></li><li><a href="#t5613.3 index.js">13.3 index.js</a></li><li><a href="#t5713.4 Header.js">13.4 Header.js</a></li><li><a href="#t5813.5 withRouter.js">13.5 withRouter.js</a></li></ul></li></ul>
    </div>
    
    <div class="content markdown-body">
        <h2 id="t01. React&#x8DEF;&#x7531;">1. React&#x8DEF;&#x7531; <a href="#t01. React&#x8DEF;&#x7531;"> # </a></h2>
<ul>
<li>&#x4E0D;&#x540C;&#x7684;&#x8DEF;&#x5F84;&#x6E32;&#x67D3;&#x4E0D;&#x540C;&#x7684;&#x7EC4;&#x4EF6;</li>
<li>&#x6709;&#x4E24;&#x79CD;&#x5B9E;&#x73B0;&#x65B9;&#x5F0F;<ul>
<li>HashRouter:&#x5229;&#x7528;hash&#x5B9E;&#x73B0;&#x8DEF;&#x7531;&#x5207;&#x6362;</li>
<li>BrowserRouter:&#x5B9E;&#x73B0;h5 Api&#x5B9E;&#x73B0;&#x8DEF;&#x7531;&#x7684;&#x5207;&#x6362;</li>
</ul>
</li>
</ul>
<h3 id="t11.1 hash">1.1 hash <a href="#t11.1 hash"> # </a></h3>
<pre><code class="lang-js">    &lt;a href=&quot;#/a&quot;&gt;&#x53BB;a&lt;/a&gt;
    &lt;a href=&quot;#/b&quot;&gt;&#x53BB;b&lt;/a&gt;
    &lt;script&gt;
      window.addEventListener(&apos;hashchange&apos;,()=&gt;{
          console.log(window.location.hash);
      });
    &lt;/script&gt;
</code></pre>
<h3 id="t21.2 history">1.2 history <a href="#t21.2 history"> # </a></h3>
<ul>
<li>history&#x5BF9;&#x8C61;&#x63D0;&#x4F9B;&#x4E86;&#x64CD;&#x4F5C;&#x6D4F;&#x89C8;&#x5668;&#x4F1A;&#x8BDD;&#x5386;&#x53F2;&#x7684;&#x63A5;&#x53E3;&#x3002;</li>
<li>history&#x5BF9;&#x8C61;&#x6301;&#x7EED;&#x8FFD;&#x8E2A;&#x7740;&#x4E00;&#x7EC4;location</li>
<li>&#x9664;&#x4E86;&#x4E00;&#x7EC4;location&#x5916;&#xFF0C;history&#x4E5F;&#x4FDD;&#x5B58;&#x4E00;&#x4E2A;&#x7D22;&#x5F15;&#x503C;&#xFF0C;&#x7528;&#x6765;&#x6307;&#x5411;&#x5F53;&#x524D;&#x6240;&#x5BF9;&#x5E94;&#x7684;location</li>
<li><a href="https://developer.mozilla.org/zh-CN/docs/Web/API/Window/history">history</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/API/History">History</a></li>
<li><a href="https://developer.mozilla.org/zh-CN/docs/Web/API/Window/onpopstate">onpopstate</a></li>
</ul>
<h2 id="t32.&#x8DD1;&#x901A;&#x8DEF;&#x7531;">2.&#x8DD1;&#x901A;&#x8DEF;&#x7531; <a href="#t32.&#x8DD1;&#x901A;&#x8DEF;&#x7531;"> # </a></h2>
<h3 id="t42.1 index.js">2.1 index.js <a href="#t42.1 index.js"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-dom&apos;</span>;
<span class="hljs-keyword">import</span> {HashRouter <span class="hljs-keyword">as</span> Router,Route} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-router-dom&apos;</span>;
<span class="hljs-keyword">import</span> Home <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./components/Home&apos;</span>;
<span class="hljs-keyword">import</span> User <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./components/User&apos;</span>;
<span class="hljs-keyword">import</span> Profile <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./components/Profile&apos;</span>;
ReactDOM.render(
    &lt;Router&gt;
        &lt;div&gt;
          &lt;Route path=&quot;/&quot; component={Home} /&gt;
          &lt;Route path=&quot;/user&quot; component={User} /&gt;
          &lt;Route path=&quot;/profile&quot; component={Profile}/&gt;
        &lt;/div&gt;
    &lt;/Router&gt;
,document.getElementById(&apos;root&apos;));
</code></pre>
<h3 id="t52.2 Home.js">2.2 Home.js <a href="#t52.2 Home.js"> # </a></h3>
<p>components/Home.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Home</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>Home<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
        )
    }
}
</code></pre>
<h3 id="t62.3 Profile.js">2.3 Profile.js <a href="#t62.3 Profile.js"> # </a></h3>
<p>components/Profile.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Profile</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>Profile<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
        )
    }
}
</code></pre>
<h3 id="t72.4 User.js">2.4 User.js <a href="#t72.4 User.js"> # </a></h3>
<p>components/User.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>User<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
        )
    }
}
</code></pre>
<h3 id="t82.5 this.props">2.5 this.props <a href="#t82.5 this.props"> # </a></h3>
<blockquote>
<p><a href="http://localhost:3000/#/user?name=zfpx#top">http://localhost:3000/#/user?name=zfpx#top</a></p>
</blockquote>
<pre><code class="lang-js">{
    <span class="hljs-string">&quot;match&quot;</span>: {
        <span class="hljs-string">&quot;path&quot;</span>: <span class="hljs-string">&quot;/user/:id&quot;</span>,   <span class="hljs-comment">//&#x5339;&#x914D;&#x8DEF;&#x5F84;</span>
        <span class="hljs-string">&quot;url&quot;</span>: <span class="hljs-string">&quot;/user/1&quot;</span>,      <span class="hljs-comment">//&#x5730;&#x5740;&#x680F;&#x4E2D;&#x7684;url</span>
        <span class="hljs-string">&quot;isExact&quot;</span>: <span class="hljs-literal">true</span>,       <span class="hljs-comment">//&#x662F;&#x5426;&#x7CBE;&#x786E;&#x5339;&#x914D;</span>
        <span class="hljs-string">&quot;params&quot;</span>: {<span class="hljs-string">&quot;id&quot;</span>: <span class="hljs-string">&quot;1&quot;</span>}  <span class="hljs-comment">// &#x8DEF;&#x5F84;&#x53C2;&#x6570;&#x5BF9;&#x8C61;</span>
    },
    <span class="hljs-string">&quot;location&quot;</span>: {
        <span class="hljs-string">&quot;pathname&quot;</span>: <span class="hljs-string">&quot;/user/1&quot;</span>,  <span class="hljs-comment">//&#x8DEF;&#x5F84;&#x540D;</span>
        <span class="hljs-string">&quot;search&quot;</span>: <span class="hljs-string">&quot;?name=zfpx&quot;</span>, <span class="hljs-comment">//&#x67E5;&#x8BE2;&#x5B57;&#x7B26;&#x4E32;</span>
        <span class="hljs-string">&quot;hash&quot;</span>: <span class="hljs-string">&quot;#top&quot;</span>          <span class="hljs-comment">//hash&#x503C;</span>
        <span class="hljs-string">&quot;state&quot;</span>:<span class="hljs-literal">undefined</span>
    },
    <span class="hljs-string">&quot;history&quot;</span>: {
        <span class="hljs-string">&quot;length&quot;</span>: <span class="hljs-number">6</span>,            <span class="hljs-comment">//&#x5386;&#x53F2;&#x957F;&#x5EA6;</span>
        <span class="hljs-string">&quot;action&quot;</span>: <span class="hljs-string">&quot;POP&quot;</span>,        <span class="hljs-comment">//&#x52A8;&#x4F5C;</span>
        <span class="hljs-string">&quot;location&quot;</span>: {           <span class="hljs-comment">//&#x5F53;&#x524D;&#x5E94;&#x7528;&#x6240;&#x5904;&#x7684;&#x4F4D;&#x7F6E;</span>
            <span class="hljs-string">&quot;pathname&quot;</span>: <span class="hljs-string">&quot;/user/1&quot;</span>,
            <span class="hljs-string">&quot;search&quot;</span>: <span class="hljs-string">&quot;?name=zfpx&quot;</span>,
            <span class="hljs-string">&quot;hash&quot;</span>: <span class="hljs-string">&quot;#top&quot;</span>,
            <span class="hljs-string">&quot;state&quot;</span>:<span class="hljs-literal">undefined</span>  <span class="hljs-comment">//location&#x53EF;&#x4EE5;&#x62E5;&#x6709;&#x4E0E;&#x4E4B;&#x76F8;&#x5173;&#x7684;&#x72B6;&#x6001;&#x3002;&#x8FD9;&#x662F;&#x4E00;&#x4E9B;&#x56FA;&#x5B9A;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x5E76;&#x4E14;&#x4E0D;&#x5B58;&#x5728;&#x4E8E;URL&#x4E4B;&#x4E2D;</span>
        },
        <span class="hljs-string">&quot;go&quot;</span>:f go(n),<span class="hljs-comment">//&#x662F;&#x4E00;&#x4E2A;&#x5F3A;&#x5927;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x5E76;&#x5305;&#x542B;&#x4E86;goForward&#x4E0E;goBack&#x7684;&#x529F;&#x80FD;&#x3002;&#x4F20;&#x5165;&#x8D1F;&#x6570;&#x5219;&#x9000;&#x540E;&#xFF0C;&#x4F20;&#x5165;&#x6B63;&#x6570;&#x5219;&#x5411;&#x524D;</span>
        <span class="hljs-string">&quot;goBack&quot;</span>:f goBack(),<span class="hljs-comment">//&#x8FD4;&#x56DE;&#x4E00;&#x5C42;&#x9875;&#x9762;&#x3002;&#x5B9E;&#x9645;&#x4E0A;&#x662F;&#x5C06;history&#x7684;&#x7D22;&#x5F15;&#x503C;&#x51CF;1</span>
        <span class="hljs-string">&quot;goForward&quot;</span>:f goForward(),<span class="hljs-comment">//&#x4E0E;goBack&#x76F8;&#x5BF9;&#x3002;&#x5411;&#x524D;&#x4E00;&#x5C42;&#x9875;&#x9762;</span>
        <span class="hljs-string">&quot;listen&quot;</span>:f listen(listener),<span class="hljs-comment">//&#x91C7;&#x7528;&#x89C2;&#x5BDF;&#x8005;&#x6A21;&#x5F0F;&#xFF0C;&#x5728;location&#x6539;&#x53D8;&#x65F6;&#xFF0C;history&#x4F1A;&#x53D1;&#x51FA;&#x901A;&#x77E5;</span>
        <span class="hljs-string">&quot;push&quot;</span>:f push(path,state),<span class="hljs-comment">//&#x65B9;&#x6CD5;&#x4F7F;&#x80FD;&#x4F60;&#x8DF3;&#x8F6C;&#x5230;&#x65B0;&#x7684;location</span>
        <span class="hljs-string">&quot;replace&quot;</span>:f replace(path,state)<span class="hljs-comment">//replace&#x65B9;&#x6CD5;&#x4E0E;push&#x76F8;&#x4F3C;&#xFF0C;&#x4F46;&#x5B83;&#x5E76;&#x975E;&#x6DFB;&#x52A0;location&#xFF0C;&#x800C;&#x662F;&#x66FF;&#x6362;&#x5F53;&#x524D;&#x7D22;&#x5F15;&#x4E0A;&#x7684;&#x4F4D;&#x7F6E;,&#x91CD;&#x5B9A;&#x5411;&#x65F6;&#x8981;&#x4F7F;&#x7528;replace,</span>
        <span class="hljs-string">&quot;createHref&quot;</span>:f createHref(location)
    }
}
</code></pre>
<h3 id="t92.6  createHref">2.6  createHref <a href="#t92.6  createHref"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> location = {
  <span class="hljs-attr">pathname</span>: <span class="hljs-string">&apos;/user&apos;</span>,
  <span class="hljs-attr">search</span>: <span class="hljs-string">&apos;?id=1&apos;</span>,
  <span class="hljs-attr">hash</span>: <span class="hljs-string">&apos;#bottom&apos;</span>
}
<span class="hljs-keyword">const</span> url = history.createHref(location)
<span class="hljs-keyword">const</span> link = <span class="hljs-built_in">document</span>.createElement(<span class="hljs-string">&apos;a&apos;</span>)
a.href = url
<span class="hljs-comment">// &lt;a href=&apos;/user?id=1#bottom&apos;&gt;&lt;/a&gt;</span>
</code></pre>
<h2 id="t103.&#x5B9E;&#x73B0;&#x57FA;&#x672C;&#x8DEF;&#x7531;">3.&#x5B9E;&#x73B0;&#x57FA;&#x672C;&#x8DEF;&#x7531; <a href="#t103.&#x5B9E;&#x73B0;&#x57FA;&#x672C;&#x8DEF;&#x7531;"> # </a></h2>
<ul>
<li><a href="https://reacttraining.com/react-router/web/example/basic">react-router</a></li>
</ul>
<h3 id="t113.1 index.js">3.1 index.js <a href="#t113.1 index.js"> # </a></h3>
<p>src/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-dom&apos;</span>;
<span class="hljs-keyword">import</span> {HashRouter <span class="hljs-keyword">as</span> Router,Route} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./react-router-dom&apos;</span>;
<span class="hljs-keyword">import</span> Home <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./components/Home&apos;</span>;
<span class="hljs-keyword">import</span> User <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./components/User&apos;</span>;
<span class="hljs-keyword">import</span> Profile <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./components/Profile&apos;</span>;
ReactDOM.render(
    &lt;Router&gt;
        &lt;div&gt;
          &lt;Route path=&quot;/&quot; component={Home} /&gt;
            &lt;Route path=&quot;/user&quot; component={User} /&gt;
          &lt;Route path=&quot;/profile&quot; component={Profile}/&gt;
        &lt;/div&gt;
    &lt;/Router&gt;
,document.getElementById(&apos;root&apos;));
</code></pre>
<h3 id="t123.2 index.js">3.2 index.js <a href="#t123.2 index.js"> # </a></h3>
<p>src/react-router-dom/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> HashRouter <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./HashRouter&apos;</span>;
<span class="hljs-keyword">import</span> Route <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./Route&apos;</span>;

<span class="hljs-keyword">export</span> {
    HashRouter,
    Route
}
</code></pre>
<h3 id="t133.3 context.js">3.3 context.js <a href="#t133.3 context.js"> # </a></h3>
<p>src/react-router-dom/context.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-comment">// React16.3</span>
<span class="hljs-keyword">let</span> {Provider,Consumer}=React.createContext();
<span class="hljs-keyword">export</span> {Provider,Consumer};
</code></pre>
<h3 id="t143.4 HashRouter.js">3.4 HashRouter.js <a href="#t143.4 HashRouter.js"> # </a></h3>
<p>src/react-router-dom/HashRouter.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {Provider} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./context&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">HashRouter</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    state={
        <span class="hljs-attr">location</span>: {
            <span class="hljs-attr">pathname</span>:<span class="hljs-built_in">window</span>.location.hash?<span class="hljs-built_in">window</span>.location.hash.slice(<span class="hljs-number">1</span>):<span class="hljs-string">&apos;/&apos;</span>
        }
    }
    componentDidMount() {
        <span class="hljs-comment">//&#x9ED8;&#x8BA4;hash&#x6CA1;&#x6709;&#x7684;&#x65F6;&#x5019;&#x8DF3;&#x8F6C;&#x5230;/</span>
        <span class="hljs-built_in">window</span>.location.hash=<span class="hljs-built_in">window</span>.location.hash||<span class="hljs-string">&apos;/&apos;</span>;
        <span class="hljs-comment">//&#x76D1;&#x542C;hash&#x503C;&#x53D8;&#x5316;</span>
        <span class="hljs-built_in">window</span>.addEventListener(<span class="hljs-string">&apos;hashchange&apos;</span>,() =&gt; {
            <span class="hljs-keyword">this</span>.setState({
                <span class="hljs-attr">location</span>: {
                ...this.state.location,
                <span class="hljs-attr">pathname</span>:<span class="hljs-built_in">window</span>.location.hash?<span class="hljs-built_in">window</span>.location.hash.slice(<span class="hljs-number">1</span>):<span class="hljs-string">&apos;/&apos;</span>
            }});
        });
    }
    render() {
        <span class="hljs-keyword">let</span> value={
            <span class="hljs-attr">location</span>:<span class="hljs-keyword">this</span>.state.location
        }
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Provider</span> <span class="hljs-attr">value</span>=<span class="hljs-string">{value}</span>&gt;</span>
                {this.props.children}
            <span class="hljs-tag">&lt;/<span class="hljs-name">Provider</span>&gt;</span></span>
        )
    }
}
</code></pre>
<h3 id="t153.5 Route.js">3.5 Route.js <a href="#t153.5 Route.js"> # </a></h3>
<p>src/react-router-dom/Route.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {Consumer} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./context&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Route</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    render() {
        <span class="hljs-keyword">let</span> {path,<span class="hljs-attr">component</span>: Component}=<span class="hljs-keyword">this</span>.props;
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Consumer</span>&gt;</span>
                {
                    value =&gt; {
                        let {pathname}=value.location;
                        if (path == pathname) {
                            return <span class="hljs-tag">&lt;<span class="hljs-name">Component</span>/&gt;</span>
                        } else {
                            return null;
                        }
                    }
                }
            <span class="hljs-tag">&lt;/<span class="hljs-name">Consumer</span>&gt;</span></span>
        )
    }
}
</code></pre>
<h2 id="t164. path-to-regexp">4. path-to-regexp <a href="#t164. path-to-regexp"> # </a></h2>
<p>&#x628A;&#x4E00;&#x4E2A;&#x8DEF;&#x5F84;&#x8F6C;&#x6362;&#x6210;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;</p>
<ul>
<li><a href="https://www.npmjs.com/package/path-to-regexp">path-to-regexp</a></li>
<li><p><a href="https://regexper.com/">regexper</a></p>
</li>
<li><p><img src="http://img.zhufengpeixun.cn/userendfalse.png" alt="&#x4E0D;&#x9700;&#x8981;&#x7ED3;&#x675F;"></p>
</li>
<li><img src="http://img.zhufengpeixun.cn/userendtrue.png" alt="&#x9700;&#x8981;&#x7ED3;&#x675F;"></li>
<li><img src="http://img.zhufengpeixun.cn/userparams.png" alt="&#x6709;&#x8DEF;&#x5F84;&#x53C2;&#x6570;"></li>
</ul>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> pathToRegexp=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path-to-regexp&apos;</span>);
<span class="hljs-comment">//let regex=pathToRegexp(&apos;/user&apos;,[],{end: true});// /^\/user(?:\/)?$/i</span>
<span class="hljs-comment">//let regex=pathToRegexp(&apos;/user&apos;,[],{end: false});// //^\/user(?:\/(?=$))?(?=\/|$)/i</span>
<span class="hljs-keyword">let</span> keys=[];
<span class="hljs-keyword">let</span> regex=pathToRegexp(<span class="hljs-string">&apos;/user/:id&apos;</span>,keys,{<span class="hljs-attr">end</span>: <span class="hljs-literal">false</span>});<span class="hljs-comment">///^\/user\/([^\/]+?)(?:\/(?=$))?(?=\/|$)/i</span>
<span class="hljs-comment">// (?:) &#x662F; &#x4E0D;&#x60F3;&#x88AB;&#x6355;&#x83B7;&#x7684;&#x65F6;&#x5019;&#x4F7F;&#x7528;</span>
<span class="hljs-comment">// (?=pattern) &#x96F6;&#x5BBD;&#x6B63;&#x5411;&#x5148;&#x884C;&#x65AD;&#x8A00;(zero-width positive lookahead assertion) </span>
<span class="hljs-built_in">console</span>.log(regex);
<span class="hljs-built_in">console</span>.log(keys);
<span class="hljs-comment">//console.log(regex.test(&apos;/user&apos;));   //true</span>
<span class="hljs-comment">//console.log(regex.test(&apos;/user/1&apos;)); //true</span>
<span class="hljs-built_in">console</span>.log(regex.test(<span class="hljs-string">&apos;/user/1&apos;</span>)); <span class="hljs-comment">//true</span>

</code></pre>
<h2 id="t175. &#x6B63;&#x5219;&#x5339;&#x914D;&#x8DEF;&#x5F84;">5. &#x6B63;&#x5219;&#x5339;&#x914D;&#x8DEF;&#x5F84; <a href="#t175. &#x6B63;&#x5219;&#x5339;&#x914D;&#x8DEF;&#x5F84;"> # </a></h2>
<h3 id="t185.1 Route.js">5.1 Route.js <a href="#t185.1 Route.js"> # </a></h3>
<p>src/react-router-dom/Route.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {Consumer} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./context&apos;</span>;
<span class="hljs-keyword">import</span> pathToRegexp <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;path-to-regexp&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Route</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    render() {
        <span class="hljs-keyword">let</span> {path,<span class="hljs-attr">component</span>: Component}=<span class="hljs-keyword">this</span>.props;
        <span class="hljs-keyword">let</span> regexp=pathToRegexp(path,[],{<span class="hljs-attr">end</span>:<span class="hljs-literal">false</span>});
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Consumer</span>&gt;</span>
                {
                    value =&gt; {
                        let {pathname}=value.location;
                        if (regexp.test(pathname)) {
                            return <span class="hljs-tag">&lt;<span class="hljs-name">Component</span>/&gt;</span>
                        } else {
                            return null;
                        }
                    }
                }
            <span class="hljs-tag">&lt;/<span class="hljs-name">Consumer</span>&gt;</span></span>
        )
    }
}
</code></pre>
<h2 id="t196.exact &#x7CBE;&#x786E;&#x5339;&#x914D;">6.exact &#x7CBE;&#x786E;&#x5339;&#x914D; <a href="#t196.exact &#x7CBE;&#x786E;&#x5339;&#x914D;"> # </a></h2>
<h3 id="t206.1 index.js">6.1 index.js <a href="#t206.1 index.js"> # </a></h3>
<p>src/index.js</p>
<pre><code class="lang-diff">ReactDOM.render(
    &lt;Router&gt;
        &lt;div&gt;
<span class="hljs-addition">+            &lt;Route exact={true} path=&quot;/&quot; component={Home} /&gt;</span>
            &lt;Route path=&quot;/user&quot; component={User} /&gt;
          &lt;Route path=&quot;/profile&quot; component={Profile}/&gt;
        &lt;/div&gt;
    &lt;/Router&gt;
,document.getElementById(&apos;root&apos;));
</code></pre>
<h3 id="t216.2  Route.js">6.2  Route.js <a href="#t216.2  Route.js"> # </a></h3>
<p>src/react-router-dom/Route.js</p>
<pre><code class="lang-diff">export default class Route extends Component{
    render() {
<span class="hljs-addition">+        let {path,component: Component,exact=false}=this.props;</span>
<span class="hljs-addition">+        let regexp=pathToRegexp(path,[],{end:exact});</span>
        return (
            &lt;Consumer&gt;
                {
                    value =&gt; {
                        let {pathname}=value.location;
                        if (regexp.test(pathname)) {
                            return &lt;Component/&gt;
                        } else {
                            return null;
                        }
                    }
                }
            &lt;/Consumer&gt;
        )
    }
}
</code></pre>
<h2 id="t227. Link">7. Link <a href="#t227. Link"> # </a></h2>
<h3 id="t237.1 Link.js">7.1 Link.js <a href="#t237.1 Link.js"> # </a></h3>
<p>src/react-router-dom/Link.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {Consumer} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./context&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Link</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Consumer</span>&gt;</span>
                {
                    value =&gt; {
                        let {history: {push}}=value;
                        return (
                            <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span>=&gt;</span>push(this.props.to)}&gt;{this.props.children}<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
                        )
                    }
                }
            <span class="hljs-tag">&lt;/<span class="hljs-name">Consumer</span>&gt;</span></span>
        )
    }
}
</code></pre>
<h3 id="t247.2 index.js">7.2 index.js <a href="#t247.2 index.js"> # </a></h3>
<p>src/index.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> <span class="hljs-string">&apos;bootstrap/dist/css/bootstrap.css&apos;</span>
ReactDOM.render(
    &lt;Router&gt;
        &lt;div&gt;
            &lt;nav className=&quot;navbar navbar-inverse&quot;&gt;
                &lt;div className=&quot;container-fluid&quot;&gt;
                    &lt;div className=&quot;navbar-header&quot;&gt;
                        &lt;a className=&quot;navbar-brand&quot; &gt;&#x5B66;&#x751F;&#x7BA1;&#x7406;&#x7CFB;&#x7EDF;&lt;/a&gt;
                    &lt;/div&gt;
                    &lt;div id=&quot;navbar&quot; className=&quot;collapse navbar-collapse&quot;&gt;
                        &lt;ul className=&quot;nav navbar-nav&quot;&gt;
                            &lt;li&gt;&lt;Link to=&quot;/&quot;&gt;Home&lt;/Link&gt;&lt;/li&gt;
                            &lt;li&gt;&lt;Link to=&quot;/user&quot;&gt;User&lt;/Link&gt;&lt;/li&gt;
                            &lt;li&gt;&lt;Link to=&quot;/profile&quot;&gt;Profile&lt;/Link&gt;&lt;/li&gt;
                        &lt;/ul&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/nav&gt;
            &lt;div className=&quot;container&quot;&gt;
                &lt;Route exact={true} path=&quot;/&quot; component={Home} /&gt;
                &lt;Route path=&quot;/user&quot; component={User} /&gt;
                &lt;Route path=&quot;/profile&quot; component={Profile}/&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/Router&gt;
,document.getElementById(&apos;root&apos;));
</code></pre>
<h3 id="t257.3 HashRouter.js">7.3 HashRouter.js <a href="#t257.3 HashRouter.js"> # </a></h3>
<p>src/react-router-dom/HashRouter.js</p>
<pre><code class="lang-js">    render() {
        <span class="hljs-keyword">let</span> value={
            <span class="hljs-attr">location</span>: <span class="hljs-keyword">this</span>.state.location,
            <span class="hljs-attr">history</span>: {
                push(to) {
                    <span class="hljs-built_in">window</span>.location.hash=to;
                }
            }
        }
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Provider</span> <span class="hljs-attr">value</span>=<span class="hljs-string">{value}</span>&gt;</span>
                {this.props.children}
            <span class="hljs-tag">&lt;/<span class="hljs-name">Provider</span>&gt;</span></span>
        )
    }
</code></pre>
<h2 id="t268.Redirect&amp;Switch">8.Redirect&amp;Switch <a href="#t268.Redirect&amp;Switch"> # </a></h2>
<h3 id="t278.1 index.js">8.1 index.js <a href="#t278.1 index.js"> # </a></h3>
<p>src/index.js</p>
<pre><code class="lang-js">&lt;div className=<span class="hljs-string">&quot;container&quot;</span>&gt;
                &lt;Switch&gt;
                    &lt;Route exact={true} path=&quot;/&quot; component={Home} /&gt;
                    &lt;Route path=&quot;/user&quot; component={User} /&gt;
                    &lt;Route path=&quot;/profile&quot; component={Profile} /&gt;
                    &lt;Redirect to=&quot;/&quot;/&gt;
                &lt;/Switch&gt;
            &lt;/div&gt;
</code></pre>
<h3 id="t288.2 Redirect.js">8.2 Redirect.js <a href="#t288.2 Redirect.js"> # </a></h3>
<p>src/react-router-dom/Redirect.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {Consumer} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./context&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Redirect</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Consumer</span>&gt;</span>
                {
                    value =&gt; {
                        value.history.push(this.props.to);
                        return null;
                    }
                }
            <span class="hljs-tag">&lt;/<span class="hljs-name">Consumer</span>&gt;</span></span>
        )
    }
}
</code></pre>
<h3 id="t298.3  Switch.js">8.3  Switch.js <a href="#t298.3  Switch.js"> # </a></h3>
<p>src/react-router-dom/Switch.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {Consumer} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./context&apos;</span>;
<span class="hljs-keyword">import</span> pathToRegexp <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;path-to-regexp&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Switch</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Consumer</span>&gt;</span>
                {
                    value =&gt; {
                        let {location: {pathname}}=value;
                        let children=this.props.children;
                        for (let i=0;i<span class="hljs-tag">&lt;<span class="hljs-name">children.length;i++){</span>
                            <span class="hljs-attr">let</span> <span class="hljs-attr">child</span>=<span class="hljs-string">children[i];</span>
                            <span class="hljs-attr">let</span> {<span class="hljs-attr">path</span>=<span class="hljs-string">&quot;/&quot;</span>,<span class="hljs-attr">exact</span>=<span class="hljs-string">false}</span>=<span class="hljs-string">child.props;</span>
                            <span class="hljs-attr">let</span> <span class="hljs-attr">regexp</span>=<span class="hljs-string">pathToRegexp(path,[],{end:</span> <span class="hljs-attr">exact</span>});
                            <span class="hljs-attr">if</span> (<span class="hljs-attr">regexp.test</span>(<span class="hljs-attr">pathname</span>)) {
                                <span class="hljs-attr">return</span> <span class="hljs-attr">child</span>;
                            }
                        }
                        <span class="hljs-attr">return</span> <span class="hljs-attr">null</span>;
                    }
                }
            &lt;/<span class="hljs-attr">Consumer</span>&gt;</span>
        )
    }
}
</span></code></pre>
<h2 id="t309.&#x9875;&#x9762;&#x8DF3;&#x8F6C;">9.&#x9875;&#x9762;&#x8DF3;&#x8F6C; <a href="#t309.&#x9875;&#x9762;&#x8DF3;&#x8F6C;"> # </a></h2>
<h3 id="t319.1  User.js">9.1  User.js <a href="#t319.1  User.js"> # </a></h3>
<p> src/components/User.js</p>
<pre><code class="lang-js"> &lt;Switch&gt;
    &lt;Route path=&quot;/user/add&quot; component={UserAdd} /&gt;
    &lt;Route path=&quot;/user/list&quot; component={UserList} /&gt;
    &lt;Route path=&quot;/user/detail/:id&quot; component={UserDetail}/&gt;
&lt;/Switch&gt;
</code></pre>
<h3 id="t329.2 HashRouter.js">9.2 HashRouter.js <a href="#t329.2 HashRouter.js"> # </a></h3>
<p>src/react-router-dom/HashRouter.js</p>
<pre><code class="lang-js">    render() {
        <span class="hljs-keyword">let</span> value={
            <span class="hljs-attr">location</span>: <span class="hljs-keyword">this</span>.state.location,
            <span class="hljs-attr">history</span>: {
                push(to) {
                    <span class="hljs-built_in">window</span>.location.hash=to;
                }
            }
        }
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Provider</span> <span class="hljs-attr">value</span>=<span class="hljs-string">{value}</span>&gt;</span>
                {this.props.children}
            <span class="hljs-tag">&lt;/<span class="hljs-name">Provider</span>&gt;</span></span>
        )
    }
</code></pre>
<h3 id="t339.3  Route.js">9.3  Route.js <a href="#t339.3  Route.js"> # </a></h3>
<p>src/react-router-dom/Route.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {Consumer} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./context&apos;</span>;
<span class="hljs-keyword">import</span> pathToRegexp <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;path-to-regexp&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Route</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    <span class="hljs-keyword">constructor</span>(props) {
        <span class="hljs-keyword">super</span>(props);
    }
    render() {
        <span class="hljs-keyword">let</span> {path,<span class="hljs-attr">component</span>: Component,exact=<span class="hljs-literal">false</span>}=<span class="hljs-keyword">this</span>.props;
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Consumer</span>&gt;</span>
                {
                    value =&gt; {
                        let {pathname}=value.location;
                        let keys=[];
                        let regexp=pathToRegexp(path,keys,{end: exact});
                        keys = keys.map(item=&gt;item.name)
                        let result=pathname.match(regexp);
                        if (result) {
                            let [,...values]=result;
                            let match={
                                params: keys.reduce((params,key,idx) =&gt; {
                                    params[key]=values[idx];
                                    return params;
                                },{}),
                                path,
                                url: pathname
                            };

                            let props={
                                location: value.location,
                                history: value.history,
                                match
                            }
                            return <span class="hljs-tag">&lt;<span class="hljs-name">Component</span> {<span class="hljs-attr">...props</span>}/&gt;</span>
                        } else {
                            return null;
                        }
                    }
                }
            <span class="hljs-tag">&lt;/<span class="hljs-name">Consumer</span>&gt;</span>
        )
    }
}
</span></code></pre>
<h3 id="t349.4  Switch.js">9.4  Switch.js <a href="#t349.4  Switch.js"> # </a></h3>
<p>src/react-router-dom/Switch.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {Consumer} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./context&apos;</span>;
<span class="hljs-keyword">import</span> pathToRegexp <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;path-to-regexp&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Switch</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Consumer</span>&gt;</span>
                {
                    value =&gt; {
                        let {location: {pathname}}=value;
                        let children=this.props.children;
                        for (let i=0;i<span class="hljs-tag">&lt;<span class="hljs-name">children.length;i++){</span>
                            <span class="hljs-attr">let</span> <span class="hljs-attr">child</span>=<span class="hljs-string">children[i];</span>
                            <span class="hljs-attr">let</span> {<span class="hljs-attr">path</span>=<span class="hljs-string">&quot;/&quot;</span>,<span class="hljs-attr">exact</span>=<span class="hljs-string">false}</span>=<span class="hljs-string">child.props;</span>
                            <span class="hljs-attr">let</span> <span class="hljs-attr">regexp</span>=<span class="hljs-string">pathToRegexp(path,[],{end:</span> <span class="hljs-attr">exact</span>});
                            <span class="hljs-attr">if</span> (<span class="hljs-attr">regexp.test</span>(<span class="hljs-attr">pathname</span>)) {
                                <span class="hljs-attr">return</span> <span class="hljs-attr">child</span>;
                            }
                        }
                        <span class="hljs-attr">return</span> <span class="hljs-attr">null</span>;
                    }
                }
            &lt;/<span class="hljs-attr">Consumer</span>&gt;</span>
        )
    }
}
</span></code></pre>
<h3 id="t359.5  UserAdd.js">9.5  UserAdd.js <a href="#t359.5  UserAdd.js"> # </a></h3>
<p>src/components/UserAdd.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UserAdd</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    handleSubmit=<span class="hljs-function">(<span class="hljs-params">event</span>) =&gt;</span> {
        event.preventDefault();
        <span class="hljs-keyword">let</span> username=<span class="hljs-keyword">this</span>.username.value;
        <span class="hljs-keyword">let</span> email=<span class="hljs-keyword">this</span>.email.value;
        <span class="hljs-keyword">let</span> user={username,email};
        <span class="hljs-keyword">let</span> usersStr=localStorage.getItem(<span class="hljs-string">&apos;users&apos;</span>);
        <span class="hljs-keyword">let</span> users=usersStr? <span class="hljs-built_in">JSON</span>.parse(usersStr):[];
        user.id = users.length&gt;<span class="hljs-number">0</span>? users[users.length<span class="hljs-number">-1</span>].id+<span class="hljs-number">1</span>:<span class="hljs-number">1</span>;
        users.push(user);
        localStorage.setItem(<span class="hljs-string">&apos;users&apos;</span>,<span class="hljs-built_in">JSON</span>.stringify(users));
        <span class="hljs-keyword">this</span>.props.history.push(<span class="hljs-string">&apos;/user/list&apos;</span>);
    }
    render() {
        <span class="hljs-keyword">return</span> (
            &lt;div className=&quot;row&quot;&gt;
                &lt;div className=&quot;col-md-12&quot;&gt;
                    &lt;form onSubmit={this.handleSubmit}&gt;
                        &lt;div className=&quot;form-group&quot;&gt;
                            &lt;label htmlFor=&quot;username&quot;&gt;&#x7528;&#x6237;&#x540D;&lt;/label&gt;
                            &lt;input type=&quot;text&quot; className=&quot;form-control&quot; ref={input=&gt;this.username = input}/&gt;
                        &lt;/div&gt;
                        &lt;div className=&quot;form-group&quot;&gt;
                            &lt;label htmlFor=&quot;email&quot;&gt;&#x90AE;&#x7BB1; &lt;/label&gt;
                            &lt;input type=&quot;email&quot; className=&quot;form-control&quot; ref={input=&gt;this.email = input}/&gt;
                        &lt;/div&gt;
                        &lt;div className=&quot;form-group&quot;&gt;
                            &lt;input type=&quot;submit&quot; className=&quot;btn btn-primary&quot;/&gt;
                        &lt;/div&gt;
                    &lt;/form&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        )
    }
}
</code></pre>
<h3 id="t369.6 UserDetail.js">9.6 UserDetail.js <a href="#t369.6 UserDetail.js"> # </a></h3>
<p>src/components/UserDetail.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UserList</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    state={
        <span class="hljs-attr">user</span>: {}
    }
    componentDidMount() {
        <span class="hljs-keyword">let</span> usersStr=localStorage.getItem(<span class="hljs-string">&apos;users&apos;</span>);
        <span class="hljs-keyword">let</span> users=usersStr? <span class="hljs-built_in">JSON</span>.parse(usersStr):[];
        <span class="hljs-keyword">let</span> user = users.find(<span class="hljs-function"><span class="hljs-params">user</span> =&gt;</span> user.id==<span class="hljs-keyword">this</span>.props.match.params.id);
        <span class="hljs-keyword">this</span>.setState({user});
    }    
    render() {
        <span class="hljs-keyword">let</span> {user}=<span class="hljs-keyword">this</span>.state;
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;row&quot;</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;col-md-12&quot;</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>ID:{user.id}<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>&#x7528;&#x6237;&#x540D;:{user.username}<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>&#x90AE;&#x7BB1;:{user.email}<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
        )
    }
}
</code></pre>
<h3 id="t379.7 UserList.js">9.7 UserList.js <a href="#t379.7 UserList.js"> # </a></h3>
<p>src/components/UserList.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{Component} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> {Link} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../react-router-dom&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UserList</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span></span>{
    state={
        <span class="hljs-attr">users</span>:[]
    }
    componentDidMount() {
        <span class="hljs-keyword">let</span> usersStr=localStorage.getItem(<span class="hljs-string">&apos;users&apos;</span>);
        <span class="hljs-keyword">let</span> users=usersStr? <span class="hljs-built_in">JSON</span>.parse(usersStr):[];
        <span class="hljs-keyword">this</span>.setState({users});
    }
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;row&quot;</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;col-md-12&quot;</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;list-group&quot;</span>&gt;</span>
                        {
                            this.state.users.map(user =&gt; (
                                <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;list-group-item&quot;</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{user.id}</span>&gt;</span>
                                    <span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">{</span>`/<span class="hljs-attr">user</span>/<span class="hljs-attr">detail</span>/${<span class="hljs-attr">user.id</span>}`}&gt;</span>{user.username}<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span>
                                <span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                            ))
                        }
                    <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
        )
    }
}
</code></pre>
<h2 id="t3810. &#x53D7;&#x4FDD;&#x62A4;&#x7684;&#x8DEF;&#x7531;">10. &#x53D7;&#x4FDD;&#x62A4;&#x7684;&#x8DEF;&#x7531; <a href="#t3810. &#x53D7;&#x4FDD;&#x62A4;&#x7684;&#x8DEF;&#x7531;"> # </a></h2>
<h3 id="t3910.1 src/index.js">10.1 src/index.js <a href="#t3910.1 src/index.js"> # </a></h3>
<pre><code class="lang-diff">                &lt;Switch&gt;
                    &lt;Route exact={true} path=&quot;/&quot; component={Home} /&gt;
                    &lt;Route path=&quot;/user&quot; component={User} /&gt;
<span class="hljs-addition">+                    &lt;Route path=&quot;/login&quot; component={Login} /&gt;</span>
<span class="hljs-addition">+                    &lt;Protected path=&quot;/profile&quot; component={Profile} /&gt;</span>
                    &lt;Redirect to=&quot;/&quot;/&gt;
                &lt;/Switch&gt;
</code></pre>
<h3 id="t4010.2 HashRouter.js">10.2 HashRouter.js <a href="#t4010.2 HashRouter.js"> # </a></h3>
<p>src/react-router-dom/HashRouter.js</p>
<pre><code class="lang-diff">render() {
        let self=this;
        let value={
            location: self.state.location,
            history: {
<span class="hljs-addition">+                push(to) {</span>
<span class="hljs-addition">+                    if (typeof to == &apos;object&apos;) {</span>
<span class="hljs-addition">+                        let {pathname,state}=to;</span>
<span class="hljs-addition">+                        self.setState({</span>
<span class="hljs-addition">+                            location: {</span>
<span class="hljs-addition">+                                ...self.state.location,state,pathname</span>
<span class="hljs-addition">+                            }</span>
<span class="hljs-addition">+                        },() =&gt; {</span>
<span class="hljs-addition">+                            window.location.hash=pathname;</span>
<span class="hljs-addition">+                        });</span>
<span class="hljs-addition">+                    } else {</span>
<span class="hljs-addition">+                        window.location.hash=to;</span>
<span class="hljs-addition">+                    }                    </span>
<span class="hljs-addition">+                }</span>
<span class="hljs-addition">+            }</span>
        }
        return (
            &lt;Provider value={value}&gt;
                {this.props.children}
            &lt;/Provider&gt;
        )
    }
</code></pre>
<h3 id="t4110.3 Route.js">10.3 Route.js <a href="#t4110.3 Route.js"> # </a></h3>
<p> src/react-router-dom/Route.js</p>
<pre><code class="lang-diff"><span class="hljs-addition">+                             if (Component) {</span>
<span class="hljs-addition">+                                return &lt;Component {...props}/&gt; </span>
<span class="hljs-addition">+                            } else if (render) {</span>
<span class="hljs-addition">+                                return render(props);</span>
<span class="hljs-addition">+                            } else {</span>
<span class="hljs-addition">+                                return null;</span>
<span class="hljs-addition">+                            }</span>
</code></pre>
<h3 id="t4210.4 Login.js">10.4 Login.js <a href="#t4210.4 Login.js"> # </a></h3>
<p>src/components/Login.js</p>
<pre><code class="lang-diff"><span class="hljs-addition">+ import React from &apos;react&apos;</span>
<span class="hljs-addition">+ export default class Login extends React.Component{</span>
<span class="hljs-addition">+    handleClick=() =&gt; {</span>
<span class="hljs-addition">+        localStorage.setItem(&apos;logined&apos;,true);</span>
<span class="hljs-addition">+        this.props.history.push(this.props.location.state.from);</span>
<span class="hljs-addition">+    }</span>
<span class="hljs-addition">+    render() {</span>
<span class="hljs-addition">+        return (</span>
<span class="hljs-addition">+            &lt;div&gt;</span>
<span class="hljs-addition">+                &lt;button</span>
<span class="hljs-addition">+                    className=&quot;btn btn-primary&quot;</span>
<span class="hljs-addition">+                    onClick={this.handleClick}</span>
<span class="hljs-addition">+                &gt;&#x767B;&#x5F55;&lt;/button&gt;</span>
<span class="hljs-addition">+            &lt;/div&gt;</span>
<span class="hljs-addition">+        )</span>
<span class="hljs-addition">+    }</span>
<span class="hljs-addition">+}</span>
</code></pre>
<h3 id="t4310.5 Protected.js">10.5 Protected.js <a href="#t4310.5 Protected.js"> # </a></h3>
<p> src/components/Protected.js</p>
<pre><code class="lang-diff"><span class="hljs-addition">+ import React from &apos;react&apos;</span>
<span class="hljs-addition">+ import {Route,Redirect} from &apos;../react-router-dom&apos;;</span>
<span class="hljs-addition">+ export default ({component:Component,...rest}) =&gt; (</span>
<span class="hljs-addition">+     &lt;Route</span>
<span class="hljs-addition">+         {...rest}</span>
<span class="hljs-addition">+         render={</span>
<span class="hljs-addition">+             props =&gt; (</span>
<span class="hljs-addition">+                 localStorage.getItem(&apos;logined&apos;)?</span>
<span class="hljs-addition">+                     &lt;Component {...props} /&gt;:</span>
<span class="hljs-addition">+                     &lt;Redirect to={{pathname: &apos;/login&apos;,state: {from: props.location.pathname}}} /&gt;</span>
<span class="hljs-addition">+             )</span>
<span class="hljs-addition">+         }</span>
<span class="hljs-addition">+     /&gt;</span>
<span class="hljs-addition">+ )</span>
</code></pre>
<h2 id="t4411. &#x81EA;&#x5B9A;&#x4E49;&#x5BFC;&#x822A;">11. &#x81EA;&#x5B9A;&#x4E49;&#x5BFC;&#x822A; <a href="#t4411. &#x81EA;&#x5B9A;&#x4E49;&#x5BFC;&#x822A;"> # </a></h2>
<h3 id="t4511.1 index.js">11.1 index.js <a href="#t4511.1 index.js"> # </a></h3>
<p>src/index.js</p>
<pre><code class="lang-js">&lt;ul className=&quot;nav navbar-nav&quot;&gt;
+    &lt;MenuLink exact to=&quot;/&quot;&gt;Home&lt;/MenuLink&gt;
+    &lt;MenuLink to=&quot;/user&quot;&gt;User&lt;/MenuLink&gt;
+    &lt;MenuLink to=&quot;/profile&quot;&gt;Profile&lt;/MenuLink&gt;
&lt;/ul&gt;
</code></pre>
<h3 id="t4611.2  Route.js">11.2  Route.js <a href="#t4611.2  Route.js"> # </a></h3>
<p> src/react-router-dom/Route.js</p>
<pre><code class="lang-js"> <span class="hljs-keyword">let</span> props={
                            <span class="hljs-attr">location</span>: value.location,
                            <span class="hljs-attr">history</span>: value.history
                        }
                        <span class="hljs-keyword">if</span> (result) {
                            <span class="hljs-keyword">let</span> [,...values]=result;
                            <span class="hljs-keyword">let</span> match={
                                <span class="hljs-attr">params</span>: keys.reduce(<span class="hljs-function">(<span class="hljs-params">params,key,idx</span>) =&gt;</span> {
                                    params[key]=values[idx];
                                    <span class="hljs-keyword">return</span> params;
                                },{}),
                                path,
                                <span class="hljs-attr">url</span>: pathname
                            };

                            props.match = match;
                            <span class="hljs-keyword">if</span> (Component) {
                                <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Component</span> {<span class="hljs-attr">...props</span>}/&gt;</span> 
                            } else if (render) {
                                return render(props);
                            } else if(children){
                                return children(props);
                            }else {
                              return null;
                            }
                        } else if(children){
                            return children(props);
                        } else {
                            return null;
                        }
</span></code></pre>
<h3 id="t4711.3 MenuLink.js">11.3 MenuLink.js <a href="#t4711.3 MenuLink.js"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>
<span class="hljs-keyword">import</span> {Route,Link} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../react-router-dom&apos;</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">&apos;./MenuLink.css&apos;</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> ({to,exact,children}) =&gt; (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Route</span>
        <span class="hljs-attr">path</span>=<span class="hljs-string">{to}</span>
        <span class="hljs-attr">exact</span>=<span class="hljs-string">{exact}</span>
        <span class="hljs-attr">children</span>=<span class="hljs-string">{</span>
            <span class="hljs-attr">props</span> =&gt;</span> (
                <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">className</span>=<span class="hljs-string">{props.match?</span>&apos;<span class="hljs-attr">active</span>&apos;<span class="hljs-attr">:</span>&apos;&apos;}&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">Link</span> <span class="hljs-attr">to</span>=<span class="hljs-string">{to}</span>&gt;</span>{children}<span class="hljs-tag">&lt;/<span class="hljs-name">Link</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
            )
        }
    /&gt;
)
</span></code></pre>
<h3 id="t4811.4  MenuLink.css">11.4  MenuLink.css <a href="#t4811.4  MenuLink.css"> # </a></h3>
<pre><code class="lang-js">.navbar-inverse .navbar-nav &gt; .active &gt; a{
    background-color: orange;
}
</code></pre>
<h2 id="t4912. &#x9632;&#x6B62;&#x8DF3;&#x8F6C;">12. &#x9632;&#x6B62;&#x8DF3;&#x8F6C; <a href="#t4912. &#x9632;&#x6B62;&#x8DF3;&#x8F6C;"> # </a></h2>
<h3 id="t5012.1 UserAdd.js">12.1 UserAdd.js <a href="#t5012.1 UserAdd.js"> # </a></h3>
<p>src/components/UserAdd.js</p>
<pre><code class="lang-diff">import React,{Component} from &apos;react&apos;;
import {Prompt} from &apos;../react-router-dom&apos;;
export default class UserAdd extends Component{
<span class="hljs-addition">+    state={</span>
<span class="hljs-addition">+        isBlocking:false</span>
<span class="hljs-addition">+    }</span>
    handleSubmit=(event) =&gt; {
<span class="hljs-addition">+        event.preventDefault();</span>
<span class="hljs-addition">+        this.setState({</span>
<span class="hljs-addition">+            isBlocking:false</span>
<span class="hljs-addition">+        },() =&gt; {</span>
            let username=this.username.value;
            let email=this.email.value;
            let user={username,email};
            let usersStr=localStorage.getItem(&apos;users&apos;);
            let users=usersStr? JSON.parse(usersStr):[];
            user.id = users.length&gt;0? users[users.length-1].id+1:1;
            users.push(user);
            localStorage.setItem(&apos;users&apos;,JSON.stringify(users));
            this.props.history.push(&apos;/user/list&apos;);
        });
    }
    render() {
<span class="hljs-addition">+        let {isBlocking}=this.state;</span>
        return (
            &lt;div className=&quot;row&quot;&gt;
                &lt;div className=&quot;col-md-12&quot;&gt;
                    &lt;form onSubmit={this.handleSubmit}&gt;
<span class="hljs-addition">+                        &lt;Prompt</span>
<span class="hljs-addition">+                            when={isBlocking}</span>
<span class="hljs-addition">+                            message={</span>
<span class="hljs-addition">+                                location=&gt;`&#x4F60;&#x786E;&#x5B9A;&#x8981;&#x8DF3;&#x8F6C;&#x5230;${location.pathname}&#x5417;&#xFF1F;`</span>
<span class="hljs-addition">+                            }</span>
<span class="hljs-addition">+                        /&gt;</span>
                        &lt;div className=&quot;form-group&quot;&gt;
                            &lt;label htmlFor=&quot;username&quot;&gt;&#x7528;&#x6237;&#x540D;&lt;/label&gt;
                            &lt;input type=&quot;text&quot;
                                onChange={
                                    event =&gt; this.setState({isBlocking:event.target.value.length&gt;0})
                                }
                                className=&quot;form-control&quot; ref={input =&gt; this.username=input} /&gt;
                        &lt;/div&gt;
                        &lt;div className=&quot;form-group&quot;&gt;
                            &lt;label htmlFor=&quot;email&quot;&gt;&#x90AE;&#x7BB1; &lt;/label&gt;
                            &lt;input
                                onChange={
                                    event =&gt; this.setState({isBlocking:event.target.value.length&gt;0})
                                }
                                type=&quot;email&quot; className=&quot;form-control&quot; ref={input =&gt; this.email=input} /&gt;
                        &lt;/div&gt;
                        &lt;div className=&quot;form-group&quot;&gt;
                            &lt;input type=&quot;submit&quot; className=&quot;btn btn-primary&quot;/&gt;
                        &lt;/div&gt;
                    &lt;/form&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        )
    }
}
</code></pre>
<h3 id="t5112.2  HashRouter.js">12.2  HashRouter.js <a href="#t5112.2  HashRouter.js"> # </a></h3>
<p>src/react-router-dom/HashRouter.js</p>
<pre><code class="lang-diff">let value={
            location: self.state.location,
            history: {
                push(to) {
<span class="hljs-addition">+                    if (self.block) {</span>
<span class="hljs-addition">+                        let allow=window.confirm(self.block(typeof to==&apos;object&apos;? to:{pathname:to}));</span>
<span class="hljs-addition">+                        if (!allow) return;</span>
<span class="hljs-addition">+                    }</span>
                    if (typeof to == &apos;object&apos;) {
                        let {pathname,state}=to;
                        self.setState({
                            location: {
                                ...self.state.location,state,pathname
                            }
                        },() =&gt; {
                            window.location.hash=pathname;
                        });
                    } else {
                        window.location.hash=to;
                    }

                },
<span class="hljs-addition">+                block(message) {</span>
<span class="hljs-addition">+                    self.block=message;</span>
<span class="hljs-addition">+                },</span>
<span class="hljs-addition">+                unblock() {</span>
<span class="hljs-addition">+                    self.block=null;</span>
<span class="hljs-addition">+                }</span>
            }
        }
</code></pre>
<h3 id="t5212.3 Prompt.js">12.3 Prompt.js <a href="#t5212.3 Prompt.js"> # </a></h3>
<p> src/react-router-dom/Prompt.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>
<span class="hljs-keyword">import</span> {Consumer} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./context&apos;</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Prompt</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span></span>{
    componentWillUnmount() {
        <span class="hljs-keyword">this</span>.history.unblock();
    }
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Consumer</span>&gt;</span>
                {
                    value =&gt; {
                        this.history=value.history;
                        const {when,message}=this.props;
                        if (when) {
                            this.history.block(message);
                        } else {
                            this.history.block(null);
                        }
                    }
                }
            <span class="hljs-tag">&lt;/<span class="hljs-name">Consumer</span>&gt;</span></span>
        );
    }
}
</code></pre>
<h2 id="t5313. withRouter">13. withRouter <a href="#t5313. withRouter"> # </a></h2>
<h3 id="t5413.1 index.js">13.1 index.js <a href="#t5413.1 index.js"> # </a></h3>
<p>rc/index.js</p>
<pre><code class="lang-diff">    &lt;div className=&quot;container-fluid&quot;&gt;
<span class="hljs-addition">+                    &lt;Header/&gt;</span>
                    &lt;div id=&quot;navbar&quot; className=&quot;collapse navbar-collapse&quot;&gt;
</code></pre>
<h3 id="t5513.2 Route.js">13.2 Route.js <a href="#t5513.2 Route.js"> # </a></h3>
<p>src/react-router-dom/Route.js</p>
<pre><code class="lang-js">+    <span class="hljs-keyword">let</span> {path=<span class="hljs-string">&apos;/&apos;</span>,<span class="hljs-attr">component</span>: Component,exact=<span class="hljs-literal">false</span>,render,children}=<span class="hljs-keyword">this</span>.props;
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Consumer</span>&gt;</span>
</span></code></pre>
<h3 id="t5613.3 index.js">13.3 index.js <a href="#t5613.3 index.js"> # </a></h3>
<p>src/react-router-dom/index.js</p>
<pre><code class="lang-diff"><span class="hljs-addition">+import withRouter from &apos;./withRouter&apos;;</span>
export {
    HashRouter,
    Route,
    Link,
    Redirect,
    Switch,
    Prompt,
<span class="hljs-addition">+    withRouter</span>
}
</code></pre>
<h3 id="t5713.4 Header.js">13.4 Header.js <a href="#t5713.4 Header.js"> # </a></h3>
<p>src/components/Header.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>
<span class="hljs-keyword">import</span> {withRouter} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../react-router-dom&apos;</span>;
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Header</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span></span>{
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">&quot;navbar-header&quot;</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">a</span>
                    <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span>=&gt;</span>this.props.history.push(&apos;/&apos;)}
                    className=&quot;navbar-brand&quot; &gt;
                   &#x5B66;&#x751F;&#x7BA1;&#x7406;&#x7CFB;&#x7EDF;
                <span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
        )
    }
}
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> withRouter(Header);
</code></pre>
<h3 id="t5813.5 withRouter.js">13.5 withRouter.js <a href="#t5813.5 withRouter.js"> # </a></h3>
<p>src/react-router-dom/withRouter.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>
<span class="hljs-keyword">import</span> {Consumer} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./context&apos;</span>;
<span class="hljs-keyword">import</span> Route <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./Route&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">Component</span>) </span>{
    <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Proxy</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span></span>{
        render() {
            <span class="hljs-keyword">return</span> (
                <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Consumer</span>&gt;</span>
                    {
                        value =&gt; {
                            return <span class="hljs-tag">&lt;<span class="hljs-name">Route</span>  <span class="hljs-attr">component</span>=<span class="hljs-string">{Component}/</span>&gt;</span>
                        }
                    }
               <span class="hljs-tag">&lt;/<span class="hljs-name">Consumer</span>&gt;</span>
            )
        }
    }
    return Proxy;
}
</span></code></pre>
<ul>
<li><a href="https://gitee.com/zhufengpeixun/router4">router4</a></li>
</ul>

        <div class="copyright">Powered by <a href="https://github.com/jaywcjlove/idoc" target="_blank">idoc</a>. Dependence <a href="https://nodejs.org">Node.js</a> run.</div>
    </div>
    
</div>

<script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.js"></script>
<script>
$('.warpper .page-toc ul ul li a').on('click',function(){
  $('.warpper .page-toc ul ul li a').removeClass('my-active')
  $(this).addClass('my-active')
})
  // if (!$('.understand-me').length) {
  //   var bar = $(window).height() - $('.navbar ').height() - $('.page-toc').position().top;
  //   var count = bar / 26 / 2;
  //   var barHeight = $('.page-toc').outerHeight();
  //   $('.page-toc li').eq(0).children('a').addClass('red');
  //   var arr = [];
  //   $("h1,h2,h3,h4,h5,h6").each(function () {
  //     arr.push($(this).position().top);
  //   });
  //   var timer
  //   function dark() {
  //     clearTimeout(timer)
  //      timer = setTimeout(function () {
  //      var top = Math.abs($('.page-toc > ul').position().top);
  //      var cur = $('.content').scrollTop();
  //      for (var i = arr.length; i >= 0; i--) {
  //        if (arr[i] <= cur) {
  //          break;
  //        }
  //      }
  //      if (i === -1) {
  //        i = 0;
  //      }
  //      $('.page-toc li a').removeClass('red');
  //      $('.page-toc li').eq(i).children('a').addClass('red');
  //      let height = $('.page-toc li').eq(i).position().top-$('.page-toc').height(); // 如果当前的offset出去了 回到中间可好？
  //      $('.page-toc').scrollTop(height+$('.page-toc').height()/2);
  //    },200)
  //   }

  //   $('.content').on('scroll', dark);
  // }
</script>
<style>

    /* ::-webkit-scrollbar{width:14px;}
    ::-webkit-scrollbar-track{background-color:transparent;}
    ::-webkit-scrollbar-thumb{background-color:transparent;}
    ::-webkit-scrollbar-thumb:hover {background-color:transparent}
    ::-webkit-scrollbar-thumb:active {background-color:transparent} */

    .page-toc > ul .red {
        background: #f3f3f3;
        z-index: 1;
        border-left: 3px solid #009a61;
        -webkit-transition: all .2s ease;
        transition: all .2s ease;
        color: #000
    }





</style>
</body>
</html>
